<template>
  <div class="dialog-container">
    <el-dialog
      v-model="dialogVisible"
      :before-close="handleClose"
      :title="title"
      width="40%"
    >
      <el-form
        ref="formRef"
        label-position="left"
        label-width="100px"
        :model="data"
      >
        <el-form-item label="商品" prop="PName">
          <el-input
            v-model="data.PName"
            :disabled="true"
            placeholder="选择商品"
            style="width: 250px"
          />
          <el-button style="margin-left: 8px" @click="addPro">
            {{ data.PName ? '修改' : '添加' }}商品
          </el-button>
        </el-form-item>
        <el-form-item label="视频名称" prop="CSVideoName">
          <el-input
            v-model="data.CSVideoName"
            placeholder="请填写视频名称"
            style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="视频类型">
          <el-select v-model="data.VideoType" placeholder="请选择">
            <el-option
              v-for="item in typeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="Remark">
          <el-input
            v-model="data.Remark"
            placeholder="请填写备注"
            style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="data.Status"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="视频上传">
          <VideoUpload
            action="/CSLogoImage/CSLogoUpload?code=csadvvideo"
            :video-url="data.CSVideo"
            @success="uploadSuccess"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
  <itemDialog
    :selected-item="data"
    title="编辑"
    :visible="itemDialogVisible"
    @close="itemDialogHandleClose"
    @confirm="itemDialogHandleConfirm"
  />
</template>

<script lang="ts" setup>
  import { ref } from 'vue'
  import VideoUpload from '@/views/components/videoUpload/index.vue'
  import itemDialog from './itemDialog.vue'

  const props = defineProps({
    visible: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: '添加商品',
    },
    type: {
      type: String,
      default: 'add',
    },
    dialogData: {
      type: Object,
      default: () => {},
    },
  })
  const dialogVisible = ref(props.visible)
  const data: any = ref(JSON.parse(JSON.stringify(props.dialogData)))
  watch(
    () => props.visible,
    () => {
      dialogVisible.value = props.visible
      if (props.visible) {
        data.value = JSON.parse(JSON.stringify(props.dialogData))
      }
    }
  )
  const typeList = [
    {
      value: '0',
      label: '横屏',
    },
    {
      value: '2',
      label: '竖屏',
    },
  ]
  const emit = defineEmits(['close', 'confirm'])
  const handleClose = () => {
    dialogVisible.value = false
    emit('close', dialogVisible.value)
  }
  const formRef: any = ref(null)
  const confirm = async () => {
    emit('confirm', data.value)
    // dialogVisible.value = false
  }
  const uploadSuccess = (url: string) => {
    data.value.CSVideo = url
  }
  // 添加商品
  const itemDialogVisible = ref(false)
  const selectedItem: any = ref({})
  const addPro = () => {
    itemDialogVisible.value = true
  }
  const itemDialogHandleClose = () => {
    itemDialogVisible.value = false
  }
  const itemDialogHandleConfirm = (result: any) => {
    selectedItem.value = result
    data.value.PName = result.Name
    data.value.PCode = result.Code
    itemDialogVisible.value = false
  }
</script>

<style lang="scss" scoped>
  .table {
  }

  .tag {
    font-size: 14px;
  }
</style>
